import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex)

export default {
    state: {
        menuList:[],
        permissionList:[],
        editableTabs: [{
            title: '首页',
            name: 'dashboard',
        }],
        editSelected: 'dashboard',
        hasRoutes: false, //菜单添加标志
    },
    mutations: {//上一级页面调用的方法（通过commit）
        //执行方法 可以传递多个参数 第一个是state
        initMenus(state,menus){
            state.menuList = menus
        },
        //权限信息
        initPermission(state,permissions){
            state.permissionList = permissions
        },
        //添加editable
        addTab(state,tab){
            state.editSelected = tab.name
            let num = state.editableTabs.findIndex(item=>item.name === tab.name)
            if (num === -1){//页面不存在 就新建标签
                state.editableTabs.push(tab)
            }
        },
        //删除editable
        removeTab(state,tabName){
            state.editableTabs.forEach((item,index)=>{
                if (item.name === tabName){
                    state.editableTabs.splice(index,1)
                }
            })
        },
        //清空editable
        clearTab(state) {
            state.editableTabs = [{title: '首页', name: 'dashboard',}]
        },
        //设置选中的menu
        setEditSelected(state,tabName){
            state.editSelected = tabName
        },
        //改变hasRoutes
        changeRouteStatue(state,hasRoutes){
            state.hasRoutes = hasRoutes
        }
    },
    actions: {//异步操作都要在actions里面
    },
    getters:{//类似于组件里的计算属性
        getMenuList(state){
            return state.menuList
        },
        getPermissionList(state){
            return state.permissionList
        },
        getEditableTabs(state){
            return state.editableTabs
        },
        //返回被选择的头部标签
        getEditSelected(state){
            return state.editSelected
        },
        getHasRoutes(state){
            return state.hasRoutes
        }
    },
}